/*
*  Copyright 2019-2025 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.server.impl;


import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import me.zhengjie.dao.MergeTaskRecordDao;
import me.zhengjie.dao.dto.MergeTaskRecordQueryCriteria;
import me.zhengjie.mapper.MergeTaskRecordMapper;
import me.zhengjie.server.MergeTaskRecordService;
import me.zhengjie.utils.FileUtil;
import me.zhengjie.utils.PageResult;
import me.zhengjie.utils.PageUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @description 服务实现
* @author laoxu
* @date 2025-06-26
**/
@Service
@RequiredArgsConstructor
public class MergeTaskRecordServiceImpl extends ServiceImpl<MergeTaskRecordMapper, MergeTaskRecordDao> implements MergeTaskRecordService {

    private final MergeTaskRecordMapper mergeTaskRecordMapper;

    @Override
    public PageResult<MergeTaskRecordDao> queryAll(MergeTaskRecordQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(mergeTaskRecordMapper.findAll(criteria, page));
    }

    @Override
    public List<MergeTaskRecordDao> queryAll(MergeTaskRecordQueryCriteria criteria){
        return mergeTaskRecordMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(MergeTaskRecordDao resources) {
        mergeTaskRecordMapper.insert(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(MergeTaskRecordDao resources) {
        MergeTaskRecordDao mergeTaskRecordDao = getById(resources.getId());
        mergeTaskRecordDao.copy(resources);
        mergeTaskRecordMapper.updateById(mergeTaskRecordDao);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<String> ids) {
        mergeTaskRecordMapper.deleteBatchIds(ids);
    }

    @Override
    public void download(List<MergeTaskRecordDao> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (MergeTaskRecordDao mergeTaskRecordDao : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("任务单号", mergeTaskRecordDao.getTaskId());
            map.put("物料条码", mergeTaskRecordDao.getItemBarCode());
            map.put("源点载具", mergeTaskRecordDao.getContainerSource());
            map.put("终点载具", mergeTaskRecordDao.getContainerTarget());
            map.put("物料重量", mergeTaskRecordDao.getWeight());
            map.put("仓库", mergeTaskRecordDao.getFloor());
            map.put("建单账号", mergeTaskRecordDao.getCreateName());
            map.put("建单时间", mergeTaskRecordDao.getCreateDate());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}